package com.jinghao.ctws.service.impl;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinghao.core.common.entity.JingHaoBaseEntity;
import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.ctws.entity.CwvCamera;
import com.jinghao.ctws.entity.CwvDataTotal;
import com.jinghao.ctws.entity.CwvFlow;
import com.jinghao.ctws.entity.CwvPallet;
import com.jinghao.ctws.entity.CwvPallet.ECwvPallet;
import com.jinghao.ctws.entity.CwvProcess;
import com.jinghao.ctws.entity.CwvProcess.ECWvProcess;
import com.jinghao.ctws.entity.CwvProcessConfig;
import com.jinghao.ctws.entity.CwvProduct;
import com.jinghao.ctws.entity.CwvProduct.ECWvProduct;
import com.jinghao.ctws.service.ICwvCameraService;
import com.jinghao.ctws.service.ICwvDataTotalService;
import com.jinghao.ctws.service.ICwvFlowService;
import com.jinghao.ctws.service.ICwvPalletService;
import com.jinghao.ctws.service.ICwvProcessConfigService;
import com.jinghao.ctws.service.ICwvProcessService;
import com.jinghao.ctws.service.ICwvProductService;
import com.jinghao.ctws.service.ICwvPublicSynchronousService;

@Service("cwvPublicSynchronousService")
public class CwvPublicSynchronousServiceImpl extends JingHaoBaseServiceImpl implements ICwvPublicSynchronousService {
	//==================定制内容开始======================
	@Resource
	private ICwvCameraService cwvCameraService;
	
	@Resource
	private ICwvFlowService cwvFlowService;
	
	@Resource
	private ICwvProcessConfigService cwvProcessConfigService;
	
	@Resource
	private ICwvPalletService cwvPalletService;
	
	@Resource
	private ICwvProcessService cwvProcessService;
	
	@Resource
	private ICwvProductService cwvProductService;
	
	@Resource
	private ICwvDataTotalService cwvDataTotalService;
	
	private String fileDirectory="/home/kl-ftp/tws/ftp/files/";
	
	//private String fileDirectory="D:/file/";
	
	
//在这里修改代码
	public String checkDataInfo(JingHaoBaseEntity model){
		if(model.getAccessTokenData()==null||model.getAccessTokenData().equals("")){
			throw new JingHaoCustomerException("请提供accessTokenData信息！");
		}
		if(model.getJhPublicData()==null||model.getJhPublicData().equals("")){
			throw new JingHaoCustomerException("请提供jhPublicData信息！");
		}
		String accessTokenData=model.getAccessTokenData();
		Map<String,Object> accessMap=(Map<String,Object>)JSON.parse(accessTokenData);
		if(accessMap==null||accessMap.get("jhactCrorgUuid")==null||accessMap.get("jhactCrorgUuid").toString().equals("")){
			throw new JingHaoCustomerException("请提供accessTokenData中未提供jhactCrorgUuid信息！");
		}
		return accessMap.get("jhactCrorgUuid").toString();
	}
	
	@Override
	public void updateSynchronousCamera(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
       String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		List<CwvCamera> list=this.updateTranslateCamera(fjhorgUuid,listDataMap);
		if(list.size()>0){
			JhCnd cnd=new JhCnd();
			cnd.andEquals("fjhorgUuid");
			CwvCamera cam=new CwvCamera();
			cam.setFjhorgUuid(fjhorgUuid);
			cam.setJhCnd(cnd);
			this.cwvCameraService.deleteSame(cam);
		}
		for(CwvCamera cwvCamera:list){
			this.cwvCameraService.add(cwvCamera);
		}
	}
	
    /**
     * 翻译摄像头信息
     * @param listDataMap
     * @param accessMap
     * @return
     */
	public List<CwvCamera> updateTranslateCamera(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		List<CwvCamera> list=new ArrayList<CwvCamera>();
		CwvCamera cwvCamera;
		Date date=null;
		for(Map<String,Object> map:listDataMap){
			cwvCamera=new CwvCamera();
			if(map.get("cameraUuid")==null||map.get("cameraUuid").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供cameraUuid信息！");
			}else{
				cwvCamera.setCameraUuid(map.get("cameraUuid").toString());
			}
			
			if(map.get("deviceId")==null||map.get("deviceId").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供deviceId信息！");
			}else{
				cwvCamera.setDeviceId(map.get("deviceId").toString());
			}
			
			if(map.get("deviceName")==null||map.get("deviceName").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供deviceName信息！");
			}else{
				cwvCamera.setDeviceName(map.get("deviceName").toString());
			}
			
			if(map.get("channelId")==null||map.get("channelId").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供channelId信息！");
			}else{
				cwvCamera.setChannelId(map.get("channelId").toString());
			}
			
			if(map.get("address")==null||map.get("address").toString().trim().equals("")){
				cwvCamera.setAddress("");
			}else{
				cwvCamera.setAddress(map.get("address").toString());
			}
			
			if(map.get("creator")==null||map.get("creator").toString().trim().equals("")){
				cwvCamera.setCreator("");
			}else{
				cwvCamera.setCreator(map.get("creator").toString());
			}
			
			if(map.get("createTime")==null||map.get("createTime").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供createTime信息！");
			}else{
				date=new Date();
				date.setTime(Long.parseLong(map.get("createTime").toString().trim()));
				cwvCamera.setCreateTime(date);
			}
			
			if(map.get("updater")==null||map.get("updater").toString().trim().equals("")){
				cwvCamera.setUpdater("");
			}else{
				cwvCamera.setUpdater(map.get("updater").toString());
			}
			
			if(map.get("updateTime")==null||map.get("updateTime").toString().trim().equals("")){
				cwvCamera.setUpdateTime(cwvCamera.getCreateTime());
			}else{
				date=new Date();
				date.setTime(Long.parseLong(map.get("updateTime").toString().trim()));
				cwvCamera.setUpdateTime(date);
			}
			
			if(map.get("status")==null||map.get("status").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供status信息！");
			}else{
				cwvCamera.setStatus(Integer.parseInt(map.get("status").toString()));
			}
			
			if(map.get("sort")==null||map.get("sort").toString().trim().equals("")){
				cwvCamera.setSort(0);
			}else{
				cwvCamera.setSort(Integer.parseInt(map.get("sort").toString()));
			}
			
			if(map.get("remark")==null||map.get("remark").toString().trim().equals("")){
				cwvCamera.setRemark("");
			}else{
				cwvCamera.setRemark(map.get("remark").toString());
			}
			cwvCamera.setFjhorgUuid(fjhorgUuid);
			cwvCamera.setUploadTime(new Date());
			list.add(cwvCamera);
		}
		return list;
	}
	@Override
	public void updateSynchronousFlow(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：删除流程环节配置信息
		JhCnd cnd=new JhCnd();
		CwvProcessConfig modelConfig=new CwvProcessConfig();
		cnd.append(" 1=1 and fflow_Uuid in (select flow_Uuid from  CWV_FLOW  where FJHORG_UUID='"+fjhorgUuid+"')");
		modelConfig.setJhCnd(cnd);
		this.cwvProcessConfigService.deleteSame(modelConfig);
		//第四步：删除流程信息
		cnd.reset();
		CwvFlow flowModel=new CwvFlow();
		flowModel.setFjhorgUuid(fjhorgUuid);
		cnd.andEquals("fjhorgUuid");
		flowModel.setJhCnd(cnd);
		this.cwvFlowService.deleteSame(flowModel);
		//第五步：保存数据
		this.updateTranslateFlow(fjhorgUuid,listDataMap);
	}
	/**
	 * 解析仓库上传的流程信息
	 * @param fjhorgUuid
	 * @param listDataMap
	 */
	public void updateTranslateFlow(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		if(listDataMap!=null&&listDataMap.size()>0){
			Date date=null;
			CwvFlow flowModel=null;
			String processConfig=null;
			List<Map<String,Object>> listProcessConfig=null;
			for(Map<String,Object> map:listDataMap){
				//第一步：保存流程环节配置信息
				flowModel=new CwvFlow();
				if(map.get("flowUuid")==null||map.get("flowUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供flowUuid信息！");
				}else{
					flowModel.setFlowUuid(map.get("flowUuid").toString());
				}
				
				if(map.get("code")==null||map.get("code").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供code信息！");
				}else{
					flowModel.setCode(map.get("code").toString());
				}
				
				if(map.get("name")==null||map.get("name").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供name信息！");
				}else{
					flowModel.setName(map.get("name").toString());
				}
				
				if(map.get("name")==null||map.get("name").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供name信息！");
				}else{
					flowModel.setName(map.get("name").toString());
				}
				
				if(map.get("creator")==null||map.get("creator").toString().trim().equals("")){
					flowModel.setCreator("");
				}else{
					flowModel.setCreator(map.get("creator").toString());
				}
				
				if(map.get("createTime")==null||map.get("createTime").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供createTime信息！");
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("createTime").toString().trim()));
					flowModel.setCreateTime(date);
				}
				
				if(map.get("updater")==null||map.get("updater").toString().trim().equals("")){
					flowModel.setUpdater("");
				}else{
					flowModel.setUpdater(map.get("updater").toString());
				}
				
				if(map.get("updateTime")==null||map.get("updateTime").toString().trim().equals("")){
					flowModel.setUpdateTime(flowModel.getCreateTime());
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("updateTime").toString().trim()));
					flowModel.setUpdateTime(date);
				}
				
				if(map.get("status")==null||map.get("status").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供status信息！");
				}else{
					flowModel.setStatus(Integer.parseInt(map.get("status").toString()));
				}
				
				if(map.get("sort")==null||map.get("sort").toString().trim().equals("")){
					flowModel.setSort(0);
				}else{
					flowModel.setSort(Integer.parseInt(map.get("sort").toString()));
				}
				
				if(map.get("remark")==null||map.get("remark").toString().trim().equals("")){
					flowModel.setRemark("");
				}else{
					flowModel.setRemark(map.get("remark").toString());
				}
				flowModel.setFjhorgUuid(fjhorgUuid);
				flowModel.setUploadTime(new Date());
				this.cwvFlowService.add(flowModel);
				//第二步：保存流程环节配置信息
				if(map.get("wvProcessConfig")!=null&&!map.get("wvProcessConfig").toString().equals("")){
					processConfig=map.get("wvProcessConfig").toString();
					listProcessConfig=(List<Map<String,Object>>)JSON.parse(processConfig);
					this.updateTranslateProcessConfig(flowModel.getFlowUuid(),listProcessConfig);
				} 
			}
		}
	}
	/**
	 * 解析仓库上传的流程环节信息
	 * @param fjhorgUuid
	 * @param listDataMap
	 */
	public void updateTranslateProcessConfig(String flowUuid,List<Map<String,Object>> listDataMap){
		if(listDataMap!=null&&listDataMap.size()>0){
			CwvProcessConfig modelConfig=null;
			Date	date=null;
			for(Map<String,Object> map:listDataMap){
				modelConfig=new CwvProcessConfig();
				if(map.get("processconfigUuid")==null||map.get("processconfigUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供processconfigUuid信息！");
				}else{
					modelConfig.setProcessconfigUuid(map.get("processconfigUuid").toString());
				}
				if(map.get("processId")==null||map.get("processId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供processId信息！");
				}else{
					modelConfig.setProcessId(map.get("processId").toString());
				}
				if(map.get("name")==null||map.get("name").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供name信息！");
				}else{
					modelConfig.setName(map.get("name").toString());
				}
				if(map.get("locationId")==null||map.get("locationId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供locationId信息！");
				}else{
					modelConfig.setLocationId(map.get("locationId").toString());
				}
				
				if(map.get("fcode")==null||map.get("fcode").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供fcode信息！");
				}else{
					modelConfig.setFcode(map.get("fcode").toString());
				}
				 
				if(map.get("creator")==null||map.get("creator").toString().trim().equals("")){
					modelConfig.setCreator("");
				}else{
					modelConfig.setCreator(map.get("creator").toString());
				}
				
				if(map.get("createTime")==null||map.get("createTime").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供createTime信息！");
				}else{
					//flowModel.setCreateTime((Date)map.get("createTime"));
				     date=new Date();
					 date.setTime(Long.parseLong(map.get("createTime").toString().trim()));
					 modelConfig.setCreateTime(date);
					// modelConfig.setUpdateTime((Date)map.get("createTime"));
				}
				
				if(map.get("updater")==null||map.get("updater").toString().trim().equals("")){
					modelConfig.setUpdater("");
				}else{
					modelConfig.setUpdater(map.get("updater").toString());
				}
				
				if(map.get("updateTime")==null||map.get("updateTime").toString().trim().equals("")){
					modelConfig.setUpdateTime(modelConfig.getCreateTime());
				}else{
				     date=new Date();
					 date.setTime(Long.parseLong(map.get("updateTime").toString().trim()));
					 modelConfig.setUpdateTime(date);
					// modelConfig.setUpdateTime((Date)map.get("updateTime"));
				}
				
				if(map.get("sort")==null||map.get("sort").toString().trim().equals("")){
					modelConfig.setSort(0);
				}else{
					modelConfig.setSort(Integer.parseInt(map.get("sort").toString()));
				}
				
				if(map.get("remark")==null||map.get("remark").toString().trim().equals("")){
					modelConfig.setRemark("");
				}else{
					modelConfig.setRemark(map.get("remark").toString());
				}
				
				if(map.get("fcameraUuid")==null||map.get("fcameraUuid").toString().trim().equals("")){
					modelConfig.setRemark("");
				}else{
					modelConfig.setFcameraUuid(map.get("fcameraUuid").toString());
				}
				modelConfig.setFflowUuid(flowUuid);
				modelConfig.setUploadTime(new Date());
				this.cwvProcessConfigService.add(modelConfig);
			}
		}
	}
	
	
	@Override
	public void updateSynchronousPallet(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateTranslatePallet(fjhorgUuid,listDataMap);
		
	}
    public void updateTranslatePallet(String fjhorgUuid,List<Map<String,Object>> listDataMap){
    	CwvPallet newModel=null;
    	if(listDataMap!=null&&listDataMap.size()>0){
    		List<CwvPallet> list=new ArrayList<CwvPallet>();
    		List<String> uuidList=new ArrayList<String>();
    		Date date=null;
    		for(Map<String,Object> map:listDataMap){
    			newModel=new CwvPallet();
				if(map.get("palletUuid")==null||map.get("palletUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供palletUuid信息！");
				}else{
					newModel.setPalletUuid(map.get("palletUuid").toString());
				}
				uuidList.add(newModel.getPalletUuid());
				if(map.get("palletId")==null||map.get("palletId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供palletId信息！");
				}else{
					newModel.setPalletId(map.get("palletId").toString());
				}
				
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					newModel.setUrlLink("");
				}else{
					newModel.setUrlLink(map.get("urlLink").toString());
				}
				
				if(map.get("urlStatus")==null||map.get("urlStatus").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlStatus信息！");
				}else{
					newModel.setUrlStatus(ECwvPallet.urlStatus_0.getKey());
				}
				newModel.setFjhorgUuid(fjhorgUuid);
				if(map.get("flowUuid")==null||map.get("flowUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供flowUuid信息！");
				}else{
					newModel.setFflowUuid(map.get("flowUuid").toString());
				}
				if(map.get("createTime")==null||map.get("createTime").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供createTime信息！");
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("createTime").toString().trim()));
					newModel.setCreateTime(date);
				}
				newModel.setUploadTime(new Date());
				list.add(newModel);
    		}
    		//第一步：删除数据
    		newModel=new CwvPallet();
    		newModel.setFjhorgUuid(fjhorgUuid);
    		newModel.setStrList(uuidList);
    		JhCnd cnd=new JhCnd();
    		cnd.andIn("palletUuid", "strList", uuidList);
    		cnd.andEquals("fjhorgUuid");
    		newModel.setJhCnd(cnd);
    		this.cwvPalletService.deleteSame(newModel);
    		//第二步：保存数据
    		for(CwvPallet tempMdoel:list){
    			this.cwvPalletService.add(tempMdoel);
    		}
    		uuidList.clear();
    		uuidList=null;
    		list.clear();
    		list=null;
    	}
    }
	@Override
	public void updateSynchronousProcess(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateSynchronousProcess(fjhorgUuid,listDataMap);
	}

	public void updateSynchronousProcess(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		CwvProcess newModel=null;
    	if(listDataMap!=null&&listDataMap.size()>0){
    		List<CwvProcess> list=new ArrayList<CwvProcess>();
    		List<String> uuidList=new ArrayList<String>();
    		Date date=null;
    		for(Map<String,Object> map:listDataMap){
    			newModel=new CwvProcess();
				if(map.get("processUuid")==null||map.get("processUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供processUuid信息！");
				}else{
					newModel.setProcessUuid(map.get("processUuid").toString());
				}
				uuidList.add(newModel.getProcessUuid());
				
				if(map.get("fpalletId")==null||map.get("fpalletId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供processUuid信息！");
				}else{
					newModel.setFpalletId(map.get("fpalletId").toString());
				}
				
				if(map.get("fprocessId")==null||map.get("fprocessId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供fprocessId信息！");
				}else{
					newModel.setFprocessId(map.get("fprocessId").toString());
				}
				
				if(map.get("startTime")==null||map.get("startTime").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供startTime信息！");
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("startTime").toString().trim()));
					newModel.setStartTime(date);
				}
				
				if(map.get("endTime")==null||map.get("endTime").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供endTime信息！");
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("endTime").toString().trim()));
					newModel.setEndTime(date);
				}
				
				if(map.get("locationId")==null||map.get("locationId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供locationId信息！");
				}else{
					newModel.setLocationId(map.get("locationId").toString());
				}
				
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					newModel.setUrlLink("");
				}else{
					newModel.setUrlLink(map.get("urlLink").toString());
				}
				
				if(map.get("urlStatus")==null||map.get("urlStatus").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlStatus信息！");
				}else{
					newModel.setUrlStatus(ECWvProcess.urlStatus_0.getKey());
				}
				
				if(map.get("palletUuid")==null||map.get("palletUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供palletUuid信息！");
				}else{
					newModel.setFpalletUuid(map.get("palletUuid").toString());
				}
				//processconfigUuid暂未实际用处，只用来反查数据使用
				if(map.get("processconfigUuid")==null||map.get("processconfigUuid").toString().trim().equals("")){
					newModel.setFprocessconfigUuid("");
				}else{
					newModel.setFprocessconfigUuid(map.get("processconfigUuid").toString());
				}
				newModel.setUploadTime(new Date());
				list.add(newModel);
    		}
    		//第一步：删除数据
    		newModel=new CwvProcess();
    		newModel.setStrList(uuidList);
    		JhCnd cnd=new JhCnd();
    		cnd.andIn("processUuid", "strList", uuidList);
    		cnd.append(" and fpallet_Uuid in (select pallet_Uuid from CWV_PALLET p where p.FJHORG_UUID='"+fjhorgUuid+"')");
    		newModel.setJhCnd(cnd);
    		this.cwvProcessService.deleteSame(newModel);
    		//第二步：保存数据
    		for(CwvProcess tempMdoel:list){
    			this.cwvProcessService.add(tempMdoel);
    		}
    		uuidList.clear();
    		uuidList=null;
    		list.clear();
    		list=null;
    	}
	}
	
	@Override
	public void updateSynchronousProduct(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateTranslateProduct(fjhorgUuid,listDataMap);
		
	}
	public void updateTranslateProduct(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		CwvProduct newModel=null;
    	if(listDataMap!=null&&listDataMap.size()>0){
    		List<CwvProduct> list=new ArrayList<CwvProduct>();
    		List<String> uuidList=new ArrayList<String>();
    		Date date=null;
    		for(Map<String,Object> map:listDataMap){
    			newModel=new CwvProduct();
				if(map.get("productUuid")==null||map.get("productUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供productUuid信息！");
				}else{
					newModel.setProductUuid(map.get("productUuid").toString());
				}
				uuidList.add(newModel.getProductUuid());
				if(map.get("batchId")==null||map.get("batchId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供batchId信息！");
				}else{
					newModel.setBatchId(map.get("batchId").toString());
				}
				if(map.get("spd")==null||map.get("spd").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供spd信息！");
				}else{
					newModel.setSpd(map.get("spd").toString());
				}
				
				if(map.get("fpalletId")==null||map.get("fpalletId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供fpalletId信息！");
				}else{
					newModel.setFpalletId(map.get("fpalletId").toString());
				}
				
				if(map.get("locationId")==null||map.get("locationId").toString().trim().equals("")){
					newModel.setLocationId("");
				}else{
					newModel.setLocationId(map.get("locationId").toString());
				}
				if(map.get("productId")==null||map.get("productId").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供productId信息！");
				}else{
					newModel.setProductId(map.get("productId").toString());
				} 
				
				if(map.get("name")==null||map.get("name").toString().trim().equals("")){
					newModel.setName("");
				}else{
					newModel.setName(map.get("name").toString());
				} 
				
				if(map.get("checkTime")==null||map.get("checkTime").toString().trim().equals("")){
					 
				}else{
					date=new Date();
					date.setTime(Long.parseLong(map.get("checkTime").toString().trim()));
					newModel.setCheckTime(date);
				}
				if(map.get("status")==null||map.get("status").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供status信息！");
				}else{
					newModel.setStatus(Integer.parseInt(map.get("status").toString()));
				} 
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					newModel.setUrlLink("");
				}else{
					newModel.setUrlLink(map.get("urlLink").toString());
				}
				
				if(map.get("urlStatus")==null||map.get("urlStatus").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlStatus信息！");
				}else{
					newModel.setUrlStatus(ECWvProduct.urlStatus_0.getKey());
				}
				if(map.get("palletUuid")==null||map.get("palletUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供palletUuid信息！");
				}else{
					newModel.setFpalletUuid(map.get("palletUuid").toString());
				}
				if(map.get("orderId")==null||map.get("orderId").toString().trim().equals("")){
					newModel.setOrderId("");
				}else{
					newModel.setOrderId(map.get("orderId").toString());
				}
				newModel.setUploadTime(new Date());
				list.add(newModel);
    		}
    		//第一步：删除数据
    		newModel=new CwvProduct();
    		newModel.setStrList(uuidList);
    		JhCnd cnd=new JhCnd();
    		cnd.andIn("productUuid", "strList", uuidList);
    		cnd.append(" and fpallet_Uuid in (select pallet_Uuid from CWV_PALLET p where p.FJHORG_UUID='"+fjhorgUuid+"')");
    		newModel.setJhCnd(cnd);
    		this.cwvProductService.deleteSame(newModel);
    		//第二步：保存数据
    		for(CwvProduct tempMdoel:list){
    			this.cwvProductService.add(tempMdoel);
    		}
    		uuidList.clear();
    		uuidList=null;
    		list.clear();
    		list=null;
    	}
	}

	@Override
	public void updateSynchronousPalletFileData(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateTranslatePalletFileData(fjhorgUuid,listDataMap);
	}
	public void updateTranslatePalletFileData(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		if(listDataMap!=null){
			CwvPallet cwvPallet=new CwvPallet();
			JhCnd cnd=new JhCnd();
			JhUpdate update=new JhUpdate();
			for(Map<String,Object> map:listDataMap){
				cnd.reset();
				if(map.get("palletUuid")==null||map.get("palletUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供palletUuid信息！");
				}else{
					cwvPallet.setPalletUuid(map.get("palletUuid").toString());
				} 
				CwvPallet tempModel=this.cwvPalletService.get(cwvPallet);
				if(tempModel==null){
					throw new JingHaoCustomerException("该托盘信息未进行上传！");
				}
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlLink信息！");
				}else{
					cwvPallet.setUrlLink(map.get("urlLink").toString());
				} 
				if(map.get("newFileName")==null||map.get("newFileName").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供newFileName信息！");
				}else{
					cwvPallet.setNewFileName(map.get("newFileName").toString());
				} 
				cwvPallet.setFjhorgUuid(fjhorgUuid);
				cwvPallet.setUrlStatus(ECwvPallet.urlStatus_1.getKey());
				cnd.andEquals("palletUuid");
				cnd.andEquals("fjhorgUuid");
				cwvPallet.setJhCnd(cnd);
				update.add("urlLink").add("urlStatus").add("newFileName");
				cwvPallet.setJhUpdate(update);
				this.cwvPalletService.updateSame(cwvPallet);
			}
		}
	}
	@Override
	public void updateSynchronousProcessFileData(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateTranslateProcessFileData(fjhorgUuid,listDataMap);
	}

	public void updateTranslateProcessFileData(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		if(listDataMap!=null){
			CwvProcess cwvProcess=new CwvProcess();
			JhCnd cnd=new JhCnd();
			JhUpdate update=new JhUpdate();
			for(Map<String,Object> map:listDataMap){
				cnd.reset();
				if(map.get("processUuid")==null||map.get("processUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供processUuid信息！");
				}else{
					cwvProcess.setProcessUuid(map.get("processUuid").toString());
				} 
				CwvProcess tempModel=this.cwvProcessService.get(cwvProcess);
				if(tempModel==null){
					throw new JingHaoCustomerException("该环节信息未进行上传！");
				}		
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlLink信息！");
				}else{
					cwvProcess.setUrlLink(map.get("urlLink").toString());
				} 
				if(map.get("newFileName")==null||map.get("newFileName").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供newFileName信息！");
				}else{
					cwvProcess.setNewFileName(map.get("newFileName").toString());
				} 
				cwvProcess.setUrlStatus(ECwvPallet.urlStatus_1.getKey());
				cnd.andEquals("processUuid");
				cnd.append(" and fpallet_Uuid in (select pallet_Uuid from CWV_PALLET p where p.FJHORG_UUID='"+fjhorgUuid+"')");
				cwvProcess.setJhCnd(cnd);
				update.add("urlLink").add("urlStatus").add("newFileName");
				cwvProcess.setJhUpdate(update);
				this.cwvProcessService.updateSame(cwvProcess);
			}
		}
	}
	@Override
	public void updateSynchronousProductFileData(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
		String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		//第三步：保存数据
		this.updateTranslateProductFileData(fjhorgUuid,listDataMap);
		
	}
	public void updateTranslateProductFileData(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		if(listDataMap!=null){
			CwvProduct cwvProduct=new CwvProduct();
			JhCnd cnd=new JhCnd();
			JhUpdate update=new JhUpdate();
			for(Map<String,Object> map:listDataMap){
				cnd.reset();
				if(map.get("productUuid")==null||map.get("productUuid").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供productUuid信息！");
				}else{
					cwvProduct.setProductUuid(map.get("productUuid").toString());
				} 
				CwvProduct tempModel=this.cwvProductService.get(cwvProduct);
				if(tempModel==null){
					throw new JingHaoCustomerException("该产品信息未进行上传！");
				}
				if(map.get("urlLink")==null||map.get("urlLink").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供urlLink信息！");
				}else{
					cwvProduct.setUrlLink(map.get("urlLink").toString());
				} 
				if(map.get("newFileName")==null||map.get("newFileName").toString().trim().equals("")){
					throw new JingHaoCustomerException("请提供newFileName信息！");
				}else{
					cwvProduct.setNewFileName(map.get("newFileName").toString());
				} 
				cwvProduct.setUrlStatus(ECwvPallet.urlStatus_1.getKey());
				cnd.andEquals("productUuid");
				cnd.append(" and fpallet_Uuid in (select pallet_Uuid from CWV_PALLET p where p.FJHORG_UUID='"+fjhorgUuid+"')");
				cwvProduct.setJhCnd(cnd);
				update.add("urlLink").add("urlStatus").add("newFileName");
				cwvProduct.setJhUpdate(update);
				this.cwvProductService.updateSame(cwvProduct);
			}
		}
	}

	@Override
	public void updateSynchronousAllPalletTjData(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
        String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		List<Map<String,Object>> listDataMap=(List<Map<String,Object>>)JSON.parse(jhPublicData);
		List<CwvDataTotal> list=this.updateTranslatePalletTj(fjhorgUuid,listDataMap);
		if(list.size()>0){
			JhCnd cnd=new JhCnd();
			cnd.andEquals("fjhorgUuid");
			CwvDataTotal dataTotal=new CwvDataTotal();
			dataTotal.setFjhorgUuid(fjhorgUuid);
			dataTotal.setJhCnd(cnd);
			this.cwvDataTotalService.deleteSame(dataTotal);
		}
		for(CwvDataTotal cwvData:list){
			this.cwvDataTotalService.add(cwvData);
		}
		
	}
	  /**
     * 翻译摄像头信息
     * @param listDataMap
     * @param accessMap
     * @return
     */
	public List<CwvDataTotal> updateTranslatePalletTj(String fjhorgUuid,List<Map<String,Object>> listDataMap){
		List<CwvDataTotal> list=new ArrayList<CwvDataTotal>();
		CwvDataTotal cwvDataTotal;
		Date date=null;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
		for(Map<String,Object> map:listDataMap){
			cwvDataTotal=new CwvDataTotal();
			cwvDataTotal.setDataUuid(cwvDataTotal.getUUID());
			cwvDataTotal.setFjhorgUuid(fjhorgUuid);
			if(map.get("twsDate")==null||map.get("twsDate").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供twsDate信息！");
			}else{
				try {
					cwvDataTotal.setDataTime(sdf.parse(map.get("twsDate").toString()));
				} catch (ParseException e) {
					throw new JingHaoCustomerException("请提供twsDate格式异常！twsDate："+map.get("twsDate").toString());
				}
			}
			if(map.get("twsDataNub")==null||map.get("twsDataNub").toString().trim().equals("")){
				throw new JingHaoCustomerException("请提供twsDataNub信息！");
			}else{
				cwvDataTotal.setDataNumber(Integer.parseInt(map.get("twsDataNub").toString()));
			}
			cwvDataTotal.setFirstCreateTime(new Date());
			cwvDataTotal.setCreateTime(new Date());
			list.add(cwvDataTotal);
		}
		return list;
	}

	@Override
	public void updateReSetPalletFile(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
        String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		Map<String,Object> dataMap=(Map<String,Object>)JSON.parse(jhPublicData);
		 
		if(dataMap!=null&&dataMap.get("palletUuid")!=null){
			String palletUuid=dataMap.get("palletUuid").toString();
			JhCnd cnd=new JhCnd();
			cnd.andEquals("palletUuid");
			CwvPallet cwvPallet=new CwvPallet();
			cwvPallet.setUrlStatus(ECwvPallet.urlStatus_0.getKey());
			cwvPallet.setPalletUuid(palletUuid);
			JhUpdate updateSet=new JhUpdate();
			updateSet.add("urlStatus");
			cwvPallet.setUpdateSet(updateSet);
			cwvPallet.setJhCnd(cnd);
			this.cwvPalletService.updateSame(cwvPallet);
			
			String fileName=dataMap.get("fileName").toString();
			boolean isDelete=this.deleteFile(this.fileDirectory+fileName+".mp4");
			if(!isDelete){
				this.logger.info("删除文件失败："+this.fileDirectory+fileName+".mp4");
			}
		}
	}
	 
	/** 
	 * 删除单个文件 
	 * @param   sPath    被删除文件的文件名 
	 * @return 单个文件删除成功返回true，否则返回false 
	 */  
	public boolean deleteFile(String sPath) {  
		Boolean flag = false;  
		File file = new File(sPath);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	        flag = true;  
	    }  
	    return flag;  
	}  
	
	@Override
	public void updateReSetProcessFile(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
        String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		Map<String,Object> dataMap=(Map<String,Object>)JSON.parse(jhPublicData);
		 
		if(dataMap!=null&&dataMap.get("processUuid")!=null){
			String processUuid=dataMap.get("processUuid").toString();
			JhCnd cnd=new JhCnd();
			cnd.andEquals("processUuid");
			CwvProcess cwvProcess=new CwvProcess();
			cwvProcess.setUrlStatus(ECwvPallet.urlStatus_0.getKey());
			cwvProcess.setProcessUuid(processUuid);
			JhUpdate updateSet=new JhUpdate();
			updateSet.add("urlStatus");
			cwvProcess.setUpdateSet(updateSet);
			cwvProcess.setJhCnd(cnd);
			this.cwvProcessService.updateSame(cwvProcess);
			
			String fileName=dataMap.get("fileName").toString();
			this.deleteFile(this.fileDirectory+fileName);
		}
		
	}

	@Override
	public void updateReSetProductFile(JingHaoBaseEntity model) {
		//第一步：检查数据是否存在
        String fjhorgUuid=this.checkDataInfo(model);
        //第二步：处理数据信息
		String jhPublicData=model.getJhPublicData();
		Map<String,Object> dataMap=(Map<String,Object>)JSON.parse(jhPublicData);
		 
		if(dataMap!=null&&dataMap.get("productUuid")!=null){
			String productUuid=dataMap.get("productUuid").toString();
			JhCnd cnd=new JhCnd();
			cnd.andEquals("productUuid");
			CwvProduct cwvProduct=new CwvProduct();
			cwvProduct.setUrlStatus(ECwvPallet.urlStatus_0.getKey());
			cwvProduct.setProductUuid(productUuid);
			JhUpdate updateSet=new JhUpdate();
			updateSet.add("urlStatus");
			cwvProduct.setUpdateSet(updateSet);
			cwvProduct.setJhCnd(cnd);
			this.cwvProductService.updateSame(cwvProduct);
			
			String fileName=dataMap.get("fileName").toString();
			this.deleteFile(this.fileDirectory+fileName);
		}
		
	}
	
	//==================定制内容结束======================
}
